package com.core.controller;

import com.core.domain.Student;
import com.core.resp.AjaxResponse;
import com.core.resp.PageResult;
import com.core.annotation.OperationLog;
import com.core.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : HUANG
 * @create : 2025/6/1 14:32
 * @function :
 */
@RestController
@RequestMapping("/student")
public class StudentController {


    private static final Logger log = LoggerFactory.getLogger(StudentController.class);
    @Autowired
    private StudentService studentService;


    @PreAuthorize("@ss.hasAuthority('student:add')")
    @PostMapping("/add")
    @OperationLog(type = "create", module = "student", description = "添加学生信息: #student.studentName")
    public AjaxResponse addStudent(@RequestBody Student student) {
        try {
            return studentService.addStudent(student);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResponse.error(405, "服务器错误，添加失败");
        }
    }

    @PreAuthorize("@ss.hasAuthority('student:get')")
    @PostMapping("/get/page/{pageSize}/{pageNum}")
    @OperationLog(type = "list", module = "student", description = "分页获取学生列表")
    public AjaxResponse getPage(@PathVariable Long pageSize, @PathVariable Long pageNum, @RequestBody Student student) {
        PageResult<Student> students = studentService.getPage(pageSize, pageNum, student);
        return AjaxResponse.success(200, "查询成功", students);
    }

    @PreAuthorize("@ss.hasAuthority('student:update')")
    @PutMapping("/update")
    @OperationLog(type = "update", module = "student", description = "更新学生信息: #student.studentName")
    public AjaxResponse update(@RequestBody Student student) {
        studentService.updateById(student);
        return AjaxResponse.success(200, "修改成功", null);
    }

    @PreAuthorize("@ss.hasAuthority('student:delete')")
    @PostMapping("/batchDelete")
    @OperationLog(type = "delete", module = "student", description = "批量删除学生信息, 学生IDs: #ids")
    public AjaxResponse batchDelete(@RequestBody List<Long> ids) {
        studentService.removeBatchByIds(ids);
        return AjaxResponse.success(200, "删除成功", null);
    }
}
